package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.user.BUserReturnVo;
import com.wanxi.education.model.vo.user.BUserVo;
import com.wanxi.education.model.vo.user.FindByIdBUserVo;
import com.wanxi.education.model.vo.user.UpdateBUserVo;
import com.wanxi.education.service.BUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: YanZhengYi
 * @Description:作用于B端用户管理页面
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Api(tags = "B端用户管理")
@RestController
@RequestMapping("/back/bUser")
@Slf4j
public class BUserController {
    @Autowired
    private BUserService bUserService;
    @GetMapping("/findPage")
    @ApiOperation("查询所有的用户数据")
    public CommonResult findPage(BUserVo userVo){
        PageInfo<BUserReturnVo> bUserReturnVo = bUserService.findPage(userVo);
        return CommonResult.success(bUserReturnVo);
    }

    @PostMapping("/update")
    @ApiOperation("更新数据")
    public CommonResult update(@RequestBody UpdateBUserVo updateBUserVo){
        Integer result = bUserService.update(updateBUserVo);
        if (result==null)return CommonResult.failure("更新失败,用户名重复");
        return CommonResult.success("success");
    }

    @PostMapping("/delete")
    @ApiOperation("删除一行")
    public CommonResult delete(Long id){
        Integer result =  bUserService.delete(id);
        if (result==null)return CommonResult.failure("删除失败");
        return CommonResult.success("success");
    }

    @PostMapping("/insert")
    @ApiOperation("添加数据")
    public CommonResult insert(@RequestBody UpdateBUserVo updateBUserVo){
        String result = bUserService.insert(updateBUserVo);
        if (!"success".equals(result))return CommonResult.failure(result);
        return CommonResult.success("success");
    }

    @GetMapping("/findById")
    @ApiOperation("通过Id进行回显数据")
    public CommonResult findById(Long id){
        FindByIdBUserVo findByIdBUserVo = bUserService.findById(id);
        return CommonResult.success(findByIdBUserVo);
    }

}
